//
//  calorieCollectionViewCell.m
//  Shine
//
//  Created by 俞青武 on 16/3/11.
//  Copyright © 2016年 王斌. All rights reserved.
//

#import "calorieCollectionViewCell.h"
#import "PNChart.h"

@implementation calorieCollectionViewCell

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView{
    self.backgroundColor = COLORBLACK;
    self.averageCalorieLabel = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH_SCREEN *0.1, 0, WIDTH_SCREEN *0.3, WIDTH_SCREEN *0.3)];
    self.averageCalorieLabel.font = [UIFont systemFontOfSize:20];
    self.averageCalorieLabel.textAlignment = NSTextAlignmentCenter;
    self.averageCalorieLabel.textColor = COLORWHITE;
    [self addSubview:self.averageCalorieLabel];
    
    
    self.allCalorieLabel = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH_SCREEN *0.55, 0, WIDTH_SCREEN *0.3, WIDTH_SCREEN *0.3)];
    self.allCalorieLabel.textAlignment = NSTextAlignmentCenter;
    self.allCalorieLabel.font = [UIFont systemFontOfSize:20];
    self.allCalorieLabel.textColor = COLORWHITE;
    [self addSubview:self.allCalorieLabel];
    
    
    self.averageCalorieNumber = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH_SCREEN *0.1, WIDTH_SCREEN *0.11, WIDTH_SCREEN *0.3, WIDTH_SCREEN *0.3)];
    self.averageCalorieNumber.textAlignment = NSTextAlignmentCenter;
    self.averageCalorieNumber.font = [UIFont systemFontOfSize:38];
    self.averageCalorieNumber.textColor = COLORYELLO;
    [self addSubview:self.averageCalorieNumber];
    
    self.allCalorieNumber = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH_SCREEN *0.55, WIDTH_SCREEN *0.11, WIDTH_SCREEN *0.3, WIDTH_SCREEN *0.3)];
    self.allCalorieNumber.textAlignment = NSTextAlignmentCenter;
    self.allCalorieNumber.font = [UIFont systemFontOfSize:38];
    self.allCalorieNumber.textColor = COLORYELLO;
    [self addSubview:self.allCalorieNumber];
    
    self.lineView = [[UIView alloc]initWithFrame:CGRectMake(55, WIDTH_SCREEN *0.64, WIDTH_SCREEN - 90, 1)];
    self.lineView.backgroundColor = [UIColor colorWithWhite:0.600 alpha:1.000];
    [self addSubview:self.lineView];
    
    self.lineView2 = [[UIView alloc]initWithFrame:CGRectMake(55, WIDTH_SCREEN, WIDTH_SCREEN - 90, 1)];
    self.lineView2.backgroundColor = [UIColor colorWithWhite:0.600 alpha:1.000];
    [self addSubview:self.lineView2];
    
    
    
    
    self.numberLabel = [[UILabel alloc]init];
    self.numberLabel.textColor = COLORYELLO;
    self.numberLabel.textAlignment = NSTextAlignmentCenter;
    self.numberLabel.font = [UIFont systemFontOfSize:25];
    [self addSubview:self.numberLabel];
    
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.contentView).with.offset(0);
        make.centerY.equalTo(self.contentView).with.offset(-150);
        make.size.mas_equalTo(CGSizeMake(200,100));
    }];
    
    
    self.barChart = [[PNBarChart alloc]initWithFrame:CGRectMake(20, WIDTH_SCREEN *0.6, WIDTH_SCREEN - 40, WIDTH_SCREEN *0.8)];
    self.barChart.backgroundColor = [UIColor clearColor];
    self.barChart.chartBorderColor = [UIColor colorWithWhite:0.902 alpha:1.000];
    
    self.barChart.yChartLabelWidth = 20.0;
    self.barChart.chartMarginLeft = 30.0;
    self.barChart.chartMarginRight = 10.0;
    self.barChart.chartMarginTop = 5.0;
    self.barChart.chartMarginBottom = 10.0;
    self.barChart.labelMarginTop = 5.0;
    self.barChart.showChartBorder = YES;
    
    //x轴数据
    self.dayArray = [NSMutableArray array];
    

    self.calorieArray = [NSMutableArray array];
    
    CAGradientLayer *gradient = [CAGradientLayer layer];
    
    gradient.colors = [NSArray arrayWithObjects:(id)[UIColor blackColor].CGColor,(id)[UIColor grayColor].CGColor,(id)[UIColor blackColor].CGColor,nil];
    [self.barChart setStrokeColor:COLORYELLO];
    self.barChart.isGradientShow = NO;//立体效果
    self.barChart.isShowNumbers = NO;//显示柱状图的数值
    [self.barChart strokeChart];
    self.barChart.delegate = self;
    
    //加载在视图上
    [self addSubview:self.barChart];

}

-(void)setDayArray:(NSMutableArray *)dayArray{
    if (_dayArray != dayArray) {
        _dayArray = dayArray;
    }
    self.arrayX = [NSArray arrayWithArray:self.dayArray];
    [self.barChart setXLabels:self.arrayX];
}

-(void)setCalorieArray:(NSMutableArray *)calorieArray{
    if (_calorieArray != calorieArray) {
        _calorieArray = calorieArray;
    }
    self.arrayY = [NSArray arrayWithArray:self.calorieArray];
    [self.barChart setYValues:_arrayY];
    [self.barChart strokeChart];

}

- (void)userClickedOnBarAtIndex:(NSInteger)barIndex
{
    
    
    
    PNBar * bar = [self.barChart.bars objectAtIndex:barIndex];
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    
    animation.fromValue = @1.0;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.toValue = @1.1;
    animation.duration = 0.2;
    animation.repeatCount = 0;
    animation.autoreverses = YES;
    animation.removedOnCompletion = YES;
    animation.fillMode = kCAFillModeForwards;
    [bar.layer addAnimation:animation forKey:@"Float"];
    //        NSLog(@"%@", self.array1[barIndex]);
    NSString *newStr = [NSString stringWithFormat:@"%@%@",@"您释放热量为",self.arrayY[barIndex]];
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:newStr];
    [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, 6)];
    
    
    
    self.numberLabel.attributedText = str;
    
    
    
    
}


@end



















